package weaver.interfaces.xfjt.utils;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author dusongsong
 */
public class XfjtUtil extends BaseBean {

    public static void main(String[] args) {
        String currentDate = DateUtil.getCurrentDate();
        String jsksrq="2023-03-27";
        int compDateVal = DateUtil.compDate(currentDate, jsksrq);
        System.out.println(compDateVal);
    }

    /**
     * 获取建模数据ID
     *
     * @return
     */
    public static String getDataidByrequestid(String requestid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from uf_dcrwtz where lcck=?", requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     * 获取建模数据ID
     *
     * @return
     */
    public static String getIdByrequestid(String requestid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from uf_xgfqtz where fqlc=?", requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
    /**
     * @Description: 将数据插入表单建模
     */
    public static int insertModelData(String tablename, int userid, Map map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        String uuid = UUID.randomUUID().toString();
        int modeid = getFormmodeId(tablename);
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if (rs.executeUpdate(insSql, params)) {
            rs1.executeQuery("select id from " + tablename + " where uuid=?", uuid);
            rs1.next();
            dataid = Util.getIntValue(rs1.getString("id"));
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get(key).toString() + "',";
                }
                if (updatesql.endsWith(",")) {
                    updatesql = updatesql.substring(0, updatesql.length() - 1);
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate(updatesql);
                }
                //进行权限重构

                getNewModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }

    /**
     * 更新数据
     *
     * @param tablename 表名
     * @param map       数据集合
     * @param dataid    更新用唯一标识
     * @return
     */
    public static boolean updateModelData(String tablename, int dataid, Map map) {
        RecordSet rs = new RecordSet();
        String sql = "update " + tablename + " set  ";
        String valuesStr = "";
        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            String key = Util.null2String(entry.getKey());
            String value = "'" + Util.null2String(entry.getValue()) + "'";
            if (("").equals(valuesStr)) {
                valuesStr = key + "=" + value + "";
            } else {
                valuesStr += "," + key + "=" + value + "";
            }
        }
        sql += valuesStr + " where id='" + dataid + "'";
        return rs.execute(sql);
    }

    /**
     * @Description: 建模权限重构
     */
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(userid, formmodeid, id);
    }

    /**
     * @Description: 获取formmodeid
     */
    public static int getFormmodeId(String tableName) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = ?", tableName);
        rs.next();
        return Util.getIntValue(rs.getString("id"));
    }

    /**
     * @Description: 物品名称重复校验
     */
    public static boolean isRepeatGoods(String mc) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(id) from uf_bgypkc where mc=?", mc);
        rs.next();
        int count = Util.getIntValue(rs.getString(1));
        return count <= 0;
    }
    /**
     * @Description: 物品名称重复校验
     */
    public static boolean isRepeatStyp(String mc) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(id) from uf_stypkc where mc=?", mc);
        rs.next();
        int count = Util.getIntValue(rs.getString(1));
        return count <= 0;
    }
    /**
     * @Description: 物品名称重复校验
     */
    public static boolean isRepeatZxbj(String mc) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(id) from uf_zxbjxx where mc=?", mc);
        rs.next();
        int count = Util.getIntValue(rs.getString(1));
        return count <= 0;
    }
    /**
     * 获取物品名称
     *
     * @param mc
     * @return
     */
    public static String getGoodsName(String mc) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select wzmc from uf_wzmc where id=?", mc);
        rs.next();
        return Util.null2String(rs.getString("wzmc"));
    }

    /**
     * 获取比价单位名称
     *
     * @param id
     * @return
     */
    public static String getBjdwname(String id) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select dwmc from uf_cgdw where id=?", id);
        rs.next();
        return Util.null2String(rs.getString("dwmc"));
    }

    public static String getBjrqyxq() {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select max(bjrqyxq) from uf_bjxxyxq where 1=1");
        rs.next();
        return Util.null2String(rs.getString(1));
    }
}
